package com.bzframework.movierecommend.controller;

import cn.hutool.json.JSONUtil;
import com.bzframework.movierecommend.client.tmdb.TMDBClient;
import com.bzframework.movierecommend.client.tmdb.config.TMDBConfiguration;
import com.bzframework.movierecommend.client.tmdb.resp.PopularMovieResp;
import com.bzframework.movierecommend.client.tmdb.resp.TMDBPageBaseResp;
import com.bzframework.movierecommend.constants.MovieConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "电影相关接口")
@RestController
@RequestMapping("/movie")
@RequiredArgsConstructor
public class MovieRecommendController {

    private final TMDBClient tmdbClient;

    private final TMDBConfiguration tmdbConfiguration;

    @GetMapping("/popular/list")
    @Operation(summary = "获取流行电影相关信息")
    public TMDBPageBaseResp listPopularMovies() {
        TMDBPageBaseResp popularMovieBaseResp = tmdbClient.getPopularMovies(MovieConstant.DEFAULT_PAGE, MovieConstant.CHINESE_LANGUAGE);
        Object results = popularMovieBaseResp.getResults();
        List<PopularMovieResp> popularMovieResp = JSONUtil.toList(JSONUtil.toJsonStr(results), PopularMovieResp.class);
        List<PopularMovieResp> popularMovieRespList = popularMovieResp.stream()
                .peek(res -> {
                    res.setPoster_path(tmdbConfiguration.withPosterUrl(res.getPoster_path()));
                    res.setBackdrop_path(tmdbConfiguration.withBackupUrl(res.getBackdrop_path()));
                }).toList();
        popularMovieBaseResp.setResults(popularMovieRespList);
        return popularMovieBaseResp;
    }

    @GetMapping("/upcoming/list")
    @Operation(summary = "获取即将上映电影相关信息")
    public TMDBPageBaseResp listUpComingMovies() {
        TMDBPageBaseResp popularMovieBaseResp = tmdbClient.getUpcomingMovies(MovieConstant.DEFAULT_PAGE, MovieConstant.CHINESE_LANGUAGE);
        Object results = popularMovieBaseResp.getResults();
        List<PopularMovieResp> popularMovieResp = JSONUtil.toList(JSONUtil.toJsonStr(results), PopularMovieResp.class);
        List<PopularMovieResp> popularMovieRespList = popularMovieResp.stream()
                .peek(res -> {
                    res.setPoster_path(tmdbConfiguration.withPosterUrl(res.getPoster_path()));
                    res.setBackdrop_path(tmdbConfiguration.withBackupUrl(res.getBackdrop_path()));
                }).toList();
        popularMovieBaseResp.setResults(popularMovieRespList);
        return popularMovieBaseResp;
    }


    @GetMapping("/top-rated/list")
    @Operation(summary = "获取评分最高的电影相关信息")
    public TMDBPageBaseResp listTopRatedMovies() {
        TMDBPageBaseResp popularMovieBaseResp = tmdbClient.getTopRatedMovies(MovieConstant.DEFAULT_PAGE, MovieConstant.CHINESE_LANGUAGE);
        Object results = popularMovieBaseResp.getResults();
        List<PopularMovieResp> popularMovieResp = JSONUtil.toList(JSONUtil.toJsonStr(results), PopularMovieResp.class);
        List<PopularMovieResp> popularMovieRespList = popularMovieResp.stream()
                .peek(res -> {
                    res.setPoster_path(tmdbConfiguration.withPosterUrl(res.getPoster_path()));
                    res.setBackdrop_path(tmdbConfiguration.withBackupUrl(res.getBackdrop_path()));
                }).toList();
        popularMovieBaseResp.setResults(popularMovieRespList);
        return popularMovieBaseResp;
    }

    @GetMapping("/now-playing/list")
    @Operation(summary = "获取正在上映电影相关信息")
    public TMDBPageBaseResp listNowPlayingMovies() {
        TMDBPageBaseResp popularMovieBaseResp = tmdbClient.getNowPlayingMovies(MovieConstant.DEFAULT_PAGE, MovieConstant.CHINESE_LANGUAGE);
        Object results = popularMovieBaseResp.getResults();
        List<PopularMovieResp> popularMovieResp = JSONUtil.toList(JSONUtil.toJsonStr(results), PopularMovieResp.class);
        List<PopularMovieResp> popularMovieRespList = popularMovieResp.stream()
                .peek(res -> {
                    res.setPoster_path(tmdbConfiguration.withPosterUrl(res.getPoster_path()));
                    res.setBackdrop_path(tmdbConfiguration.withBackupUrl(res.getBackdrop_path()));
                }).toList();
        popularMovieBaseResp.setResults(popularMovieRespList);
        return popularMovieBaseResp;
    }


}
